using System.Collections;
using AdvancedInspector;
using Holoville.HOTween;
using I2.Loc;
using UnityEngine;

[AdvancedInspector(true)]
public class IndicatorWinPvP : MonoBehaviour
{
	public static IndicatorWinPvP instance;

	public UILabel label;

	public GameObject lightning;

	public float animationTimeBeforeParticle = 2f;

	public float stayOnScreenTime = 2f;

	public float animationTimeAfterParticle = 2f;

	public Vector3 Scale;

	public string message = string.Empty;

	[HideInInspector]
	public float timeToSeconMessage;

	public bool IsAnimationPlay;

	[HideInInspector]
	public float _alpha;

	private bool needSecondMessage;

	private void Awake()
	{
		instance = this;
		timeToSeconMessage = animationTimeBeforeParticle + animationTimeAfterParticle + stayOnScreenTime;
	}

	public void ActivateIndicator(string text, bool showSecondTxt = true)
	{
		message = text;
		needSecondMessage = showSecondTxt;
		if ((LevelUpIndicator.instance != null && LevelUpIndicator.instance.IsAnimationPlay) || (MissionCompleted.instance != null && MissionCompleted.instance.IsAnimationPlay))
		{
			StartCoroutine(WaitAnimation());
		}
		else
		{
			StartIndicator();
		}
	}

	private IEnumerator WaitAnimation()
	{
		yield return new WaitForSeconds(1f);
		if (LevelUpIndicator.instance.IsAnimationPlay || MissionCompleted.instance.IsAnimationPlay)
		{
			StartCoroutine(WaitAnimation());
		}
		else
		{
			StartIndicator();
		}
	}

	private void Start()
	{
		HOTween.Init();
		Reset();
	}

	private void OnDisable()
	{
		StopAllCoroutines();
	}

	private void OnDestroy()
	{
		instance = null;
		StopAllCoroutines();
	}

	private void Update()
	{
		if (IsAnimationPlay)
		{
			label.alpha = _alpha;
		}
	}

	[Inspect]
	private void StartIndicator()
	{
		if (ExpHUD.instance != null)
		{
			ExpHUD.instance.SetLevelLabel(1);
		}
		if (!IsAnimationPlay)
		{
			IsAnimationPlay = true;
			label.text = ScriptLocalization.Get("Game_PVP_msg/src_MatchOver");
			if (!needSecondMessage)
			{
				label.text = message;
			}
			label.gameObject.SetActive(true);
			label.transform.localScale = Vector3.one;
			Scale = new Vector3(0.4f, 0.4f, 0.4f);
			HOTween.Kill(label.transform);
			HOTween.To(label.transform, animationTimeBeforeParticle, new TweenParms().Prop("localScale", Scale).UpdateType(UpdateType.TimeScaleIndependentUpdate).OnComplete(DeactivateFirstIndicator));
			HOTween.To(this, animationTimeBeforeParticle, new TweenParms().Prop("_alpha", 1f));
		}
	}

	public void DeactivateFirstIndicator()
	{
		Invoke("WaitForFirstMessageEnd", stayOnScreenTime);
	}

	public void WaitForFirstMessageEnd()
	{
		HOTween.Kill(label.transform);
		HOTween.To(label.transform, animationTimeAfterParticle, new TweenParms().Prop("localScale", Vector3.one * 0.01f).UpdateType(UpdateType.TimeScaleIndependentUpdate).OnComplete(WaitForSecondMessage));
		HOTween.To(this, animationTimeAfterParticle, new TweenParms().Prop("_alpha", 0f));
	}

	public void WaitForSecondMessage()
	{
		if (needSecondMessage)
		{
			label.alpha = _alpha;
			label.transform.localScale = Vector3.one;
			lightning.SetActive(false);
			label.gameObject.SetActive(false);
			Invoke("SecondMesage", 0.1f);
		}
	}

	public void SecondMesage()
	{
		label.text = message;
		label.gameObject.SetActive(true);
		label.transform.localScale = Vector3.one;
		Scale = new Vector3(0.6f, 0.6f, 0.6f);
		HOTween.Kill(label.transform);
		HOTween.To(label.transform, animationTimeBeforeParticle, new TweenParms().Prop("localScale", Scale).UpdateType(UpdateType.TimeScaleIndependentUpdate).OnComplete(DeactivateSecondIndicator));
		HOTween.To(this, animationTimeBeforeParticle, new TweenParms().Prop("_alpha", 1f));
	}

	public void DeactivateSecondIndicator()
	{
		Invoke("WaitForSecondMessageEnd", stayOnScreenTime);
	}

	public void WaitForSecondMessageEnd()
	{
		HOTween.Kill(label.transform);
		HOTween.To(label.transform, animationTimeAfterParticle, new TweenParms().Prop("localScale", Vector3.one * 0.01f).UpdateType(UpdateType.TimeScaleIndependentUpdate).OnComplete(Reset));
		HOTween.To(this, animationTimeAfterParticle, new TweenParms().Prop("_alpha", 0f));
	}

	public void Reset()
	{
		IsAnimationPlay = false;
		StopAllCoroutines();
		_alpha = 0f;
		label.alpha = _alpha;
		label.transform.localScale = Vector3.one;
		lightning.SetActive(false);
		label.gameObject.SetActive(false);
	}
}
